﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Telerik.WinControls;
using Telerik.WinControls.UI;

namespace UnigenReport.Form
{
    public partial class ucWO : UserControl
    {
        private UnigenDataContext db;

        #region Thiết lập

        public ucWO()
        {
            InitializeComponent();
            SetEvent();
            Common.SetMenuShortcut(btnInsert, btnUpdate, btnDelete, btnGetData, btnCloseForm);
        }

        /// <summary>
        /// Khai báo các sự kiện
        /// </summary>
        private void SetEvent()
        {
            this.Load += OnLoad;
            this.btnInsert.Click += new System.EventHandler(this.btnInsert_Click);
            this.btnUpdate.Click += new System.EventHandler(this.btnUpdate_Click);
            this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
            this.btnGetData.Click += new System.EventHandler(this.btnGetData_Click);
            this.btnCloseForm.Click += new System.EventHandler(this.btnCloseForm_Click);
            this.btnSearch.Click += new System.EventHandler(this.btnSearch_Click);
            this.grvWO.CellDoubleClick += new Telerik.WinControls.UI.GridViewCellEventHandler(this.grvWO_CellDoubleClick);        
        }

        /// <summary>
        /// Hàm load form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnLoad(object sender, EventArgs e)
        {
            //Load default
            SetDefault();
        }

        /// <summary>
        /// Thiết lập các giá trị mặc định của form
        /// </summary>
        private void SetDefault()
        {
            //Source unit
            db = new UnigenDataContext();
            sourceUnit.DataSource = db.Units;

            //Bind dữ liệu chính
            BindData();
        }

        /// <summary>
        /// Lấy dữ liệu chính
        /// </summary>
        private void BindData()
        {
            //Data bind
            db = new UnigenDataContext();
            var code = txtCode.Text.Trim();
            if(String.IsNullOrEmpty(code))
                sourceWO.DataSource = db.WorldOrders.Where(p => p.Active == true).OrderBy(p => p.WO).ToList();
            else
                sourceWO.DataSource = db.WorldOrders.Where(p => p.Active == true && p.WO.Contains(code))
                                                                .OrderBy(p => p.WO).ToList();

            //STT
            Common.SetNo(grvWO);
        }

        #endregion

        #region Hàm

        private void Insert()
        {
            try
            {
                var newItem = new Form.ucWOEdit() { Dock = DockStyle.Fill };
                var ui = Common.CreateForm(newItem, String.Empty, 460, 390);

                if (ui.ShowDialog() == DialogResult.OK)
                {
                    BindData();
                    Common.ShowSaveSuccess();
                }
            }
            catch (Exception ex)
            {
                Common.ShowErrorMess("Insert - " + ex.Message);
            }
        }

        private void Update()
        {
            try
            {
                if (sourceWO.Current == null)
                {
                    Common.ShowSelectionRequire();
                    return;
                }

                var p = sourceWO.Current as WorldOrder;

                var itemEdit = new Form.ucWOEdit() { Dock = DockStyle.Fill }.ForUpdate(p);
                var ui = Common.CreateForm(itemEdit, String.Empty, 460, 390);

                if (ui.ShowDialog() == DialogResult.OK)
                {
                    BindData();
                    Common.ShowSaveSuccess();
                }
            }
            catch (Exception ex)
            {
                Common.ShowErrorMess("Update - " + ex.Message);
            }
        }

        private void Delete()
        {
            try
            {
                if(Common.ShowQuestion("Xóa dòng này?") != DialogResult.OK)
                    return;

                if (sourceWO.Current == null)
                {
                    Common.ShowSelectionRequire();
                    return;
                }

                db = new UnigenDataContext();
                var input = (sourceWO.Current as Product);
                var delete = db.WorldOrders.SingleOrDefault(p => p.WOId == input.UnitId);
                //Xóa
                delete.Active = false;
                db.SubmitChanges();

                //Remove khỏi grid
                sourceWO.Remove(input);

                //Thông báo
                Common.ShowSaveSuccess();
            }
            catch (Exception ex)
            {
                Common.ShowErrorMess("Delete - " + ex.Message);
            }
        }

        #endregion

        #region Sự kiện

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.Enter)
            {
                BindData();
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            Insert();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            Update();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            Delete();
        }

        private void btnGetData_Click(object sender, EventArgs e)
        {
            BindData();
        }

        private void btnCloseForm_Click(object sender, EventArgs e)
        {
            (this.Parent as Telerik.WinControls.UI.RadPageViewPage).Dispose();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            BindData();
        }

        private void grvWO_CellDoubleClick(object sender, GridViewCellEventArgs e)
        {
            Update();
        }

        #endregion
    }
}
